package com.fansl.allround.open.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import com.alibaba.fastjson.JSONObject;
import com.fansl.allround.open.api.constant.enums.juhe.NewsTypeEnum;
import com.fansl.allround.open.api.constant.enums.juhe.WeatherWidEnum;
import com.fansl.allround.open.api.entity.sms.SendSmsCodePO;
import com.fansl.allround.open.api.entity.juhe.*;
import com.fansl.allround.open.api.entity.sms.SendSmsMessage;
import com.fansl.allround.open.api.vo.NewsHeadlinesVO;
import com.fansl.allround.open.api.vo.SmsCodeVO;
import com.fansl.allround.open.api.vo.WeatherVO;
import com.fansl.allround.open.helper.sms.SmsMessageTemplateContext;
import com.fansl.allround.open.invoker.JuHeApiInvoker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author fansl
 * @Description: TODO
 * @date 2019/9/10 11:08
 */
@Slf4j
@Service
public class JuHeService {
    @Resource
    private JuHeApiInvoker juHeApiInvoker;
    /**
     * 获取新闻头条
     * @param newsType
     * @return
     */
    @Cacheable(value = "juhe_newsheadlines", key = "#newsType.name()")
    public List<NewsHeadlinesVO> getNewsHeadlines(NewsTypeEnum newsType) {
        List<NewsHeadlinesVO> list = new ArrayList<>();
        JeHeResult<NewsHeadlinesResult> result = juHeApiInvoker.getNewsHeadlines(newsType);
        NewsHeadlinesResult newsHeadlinesResult = result.getResult();
        if(null==newsHeadlinesResult){
            log.error("[聚合api-获取新闻头条]异常,result:{}",JSONObject.toJSONString(result));
            return list;
        }
        newsHeadlinesResult.getData().forEach(newsHeadlinesCard -> {
            NewsHeadlinesVO newsHeadlinesVO = new NewsHeadlinesVO();
            newsHeadlinesVO.setTitle(newsHeadlinesCard.getTitle());
            newsHeadlinesVO.setAuthorName(newsHeadlinesCard.getAuthor_name());
            newsHeadlinesVO.setCategory(newsHeadlinesCard.getCategory());
            newsHeadlinesVO.setDate(newsHeadlinesCard.getDate());
            newsHeadlinesVO.setUrl(newsHeadlinesCard.getUrl());
            List<String> thumbnailPicList = new ArrayList<>();
            if(StrUtil.isNotBlank(newsHeadlinesCard.getThumbnail_pic_s())){
                thumbnailPicList.add(newsHeadlinesCard.getThumbnail_pic_s());
            }
            if(StrUtil.isNotBlank(newsHeadlinesCard.getThumbnail_pic_s02())){
                thumbnailPicList.add(newsHeadlinesCard.getThumbnail_pic_s02());
            }
            if(StrUtil.isNotBlank(newsHeadlinesCard.getThumbnail_pic_s03())){
                thumbnailPicList.add(newsHeadlinesCard.getThumbnail_pic_s03());
            }
            newsHeadlinesVO.setThumbnailPicList(thumbnailPicList);
            list.add(newsHeadlinesVO);
        });
        return list;
    }

    /**
     * 获取天气情况
     * @param cityName
     * @return
     */
    @Cacheable(value = "juhe_weather", key = "#cityName.hashCode()")
    public WeatherVO getWeather(String cityName) {
        WeatherVO weatherVO = new WeatherVO();
        JeHeResult<WeatherResult> result = juHeApiInvoker.getWeather(cityName);
        WeatherResult weatherResult = result.getResult();
        if(null==weatherResult){
            log.error("[聚合api-获取天气情况]异常,result:{}",JSONObject.toJSONString(result));
            return weatherVO;
        }
        weatherVO.setCity(weatherResult.getCity());
        //今日天气
        WeatherRealtime realtime = new WeatherRealtime();
        WeatherRealtime weatherRealtime = weatherResult.getRealtime();
        BeanUtil.copyProperties(weatherRealtime,realtime);
        realtime.setWid(WeatherWidEnum.getByName(weatherRealtime.getWid()).getDesc());
        weatherVO.setRealtime(realtime);
        //未来天气
        List<WeatherFuture> futureList = new ArrayList<>();
        List<WeatherFuture> weatherFutureList = weatherResult.getFuture();
        weatherFutureList.forEach(weatherFuture -> {
            WeatherFuture future = new WeatherFuture();
            BeanUtil.copyProperties(weatherFuture,future);
            WeatherWid weatherWid = new WeatherWid();
            weatherWid.setDay(WeatherWidEnum.getByName(weatherFuture.getWid().getDay()).getDesc());
            weatherWid.setNight(WeatherWidEnum.getByName(weatherFuture.getWid().getNight()).getDesc());
            future.setWid(weatherWid);
            futureList.add(future);
        });
        futureList.sort(Comparator.comparingLong(future-> DateUtil.parse(future.getDate(),"yyyy-MM-dd").getTime()));
        weatherVO.setFuture(futureList);
        return weatherVO;
    }

    /**
     * 发送短信验证码
     * @param smsCodePO
     * @return
     */
    public SmsCodeVO sendSmsCode(SendSmsCodePO smsCodePO) {
        SmsCodeVO smsCodeVO = new SmsCodeVO();

        SmsMessageTemplateContext smsMessageTemplateContext = new SmsMessageTemplateContext(smsCodePO);
        try {
            SendSmsMessage smsMessage = smsMessageTemplateContext.execute();
//            JeHeResult<SmsResult> result = juHeApiInvoker.sendSmsCode(smsMessage.getMobile(),smsMessage.getTplId(),
//                    smsMessage.getTplValue());
//            if (null==result.getResult()){
//                log.error("[聚合api-发送短信验证码]异常,result:{}",JSONObject.toJSONString(result));
//                return smsCodeVO;
//            }
            smsCodeVO.setCode(smsMessage.getCode());
            smsCodeVO.setSmsCodeType(smsCodePO.getSmsCodeType());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("发送手机消息错误",e);
        }
        return smsCodeVO;
    }
}
